package com.eati.cm.hook_like;

import com.eati.cm.excp.IWebErr;
import com.eati.cm.view.IResult;
import com.eati.cm.view.Result;
import com.eati.cm.view.ResultStatus;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Date;

@RestControllerAdvice
public class UnifiedExcpHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public IResult onExcp(Exception e){
        Result<Object> res = new Result<>();
        if(e instanceof IWebErr){
            res.status = ((IWebErr) e).getResultStatus();
        }else {
            res.status = ResultStatus.PROG_ERR;
        }
        res.finishedDate = new Date();
        res.opMsg = e;
        res.opMsgType = e.getClass().toString();
        return res;
    }
}
